<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_User extends CI_Model{
	
	function getAll(){
		$this->db->join('fa_role', 'fa_role.id_role = fa_admin.id_role');
        $rs = $this->db->get('fa_admin');
        
        return $rs->result();
	}

    function getByJabatan($strId){
        $strSQL = "SELECT * FROM tb_users u, tb_jabatan j WHERE u.id_jabatan = j.id_jabatan AND u.id_jabatan = ?";
        $strQuery = $this->db->query($strSQL,$strId);
        if($strQuery->num_rows() > 0){
            foreach ($strQuery->result_array() as $arrRow) {
                $arrStrResult[] = $arrRow;
            }
            $strQuery->free_result();
            return $arrStrResult;
        }else{
            return array();
        }
    }

	function getOneById($strId){
		$strSQL = "SELECT * FROM tb_users WHERE username = ?";
		$strQuery = $this->db->query($strSQL,$strId);
		if($strQuery->num_rows() > 0){
            $arrResult = $strQuery->row_array();
            $strQuery->free_result();
            return $arrResult;
        }else{
            return array();
        }
	}
	function getOneByEmail($strId){
		$strSQL = "SELECT * FROM tb_users WHERE email = ?";
		$strQuery = $this->db->query($strSQL,$strId);
		if($strQuery->num_rows() > 0){
            $arrResult = $strQuery->row_array();
            $strQuery->free_result();
            return $arrResult;
        }else{
            return array();
        }
	}

	function insertUser($arrParam){
		$boolStatus = FALSE;
		$this->db->where('username',$arrParam[0]);
        $this->db->or_where('email',$arrParam[1]);
        $this->db->from("tb_users");
        $count = $this->db->count_all_results();
        if($count > 0){
            $boolStatus = FALSE;
        }else{
            $strSQL = "INSERT INTO tb_users(username,password,email,full_name,id_jabatan) VALUES(?,?,?,?,?)";
            if($this->db->query($strSQL, $arrParam)){
                $boolStatus = TRUE;
            }else{
                $boolStatus = FALSE;
            }
        }
		return $boolStatus;
	}

    function deleteUser($strId){
        $boolStatus = false;
        $strSQL = "DELETE FROM tb_users WHERE id_user =?";
        if($this->db->query($strSQL, $strId)){
            $boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
        return $boolStatus;
    }

    function resetPassword($arrParam){
        $boolStatus = false;
        $strSQL = "UPDATE tb_users SET password = ? WHERE id_user =?";
        if($this->db->query($strSQL, $arrParam)){
            $boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
        return $boolStatus;
    }

}
/* End of file m_user.php */
/* Location: .models/m_user.php */